package com.github.edu.web.roleUser.controller;

import com.github.admin.edu.assembly.common.entity.JsonArray;
import com.github.admin.edu.assembly.page.entity.Pager;
import com.github.admin.edu.oauth.server.api.entity.TSysRoleUser;
import com.github.admin.edu.oauth.server.api.entity.TSysUser;
import com.github.edu.client.common.annotation.*;
import com.github.edu.client.common.controller.BaseController;
import com.github.edu.web.roleUser.service.RoleUserManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by IntelliJ IDEA.
 * user:wangj
 * date:2018/8/12
 * Time: 19:32
 */
@Controller
@RequestMapping("/uc/roleUser")
public class RoleUserController extends BaseController<TSysRoleUser,Long> {

    @Autowired
    private RoleUserManagerService service;

    @ModelAttribute
    public void addTag(ModelMap modelMap){
        modelMap.put("userList",service.getAllUser());
        modelMap.put("roleList",service.getAllRole());
        modelMap.put("orgList",service.getAllOrganization());
    }

    @QueryAll
    public String index(ModelMap modelMap, TSysRoleUser sysOauth, Pager pager) {
        if(null==pager){
            pager=new Pager();
        }
        pager.setDirection("ASC");
        String[] strs={"roleid"};
        pager.setOrder(strs);
        JsonArray<TSysRoleUser> jsonArray=service.getAllByPage(sysOauth,pager);
        return super.index(modelMap,sysOauth,jsonArray,"roleUser","岗位成员管理",
                "编码:id","用户姓名:username","用户角色:rolename","所属部门:orgname");
    }

    @New
    public String add(ModelMap modelMap, TSysRoleUser sysOauth, HttpServletRequest request) {
        return super.newIndex(modelMap,sysOauth,"roleUser","新增岗位成员");
    }

    @Save
    public String saveOrUpdate(ModelMap modelMap, TSysRoleUser sysOauth, HttpServletRequest request) {
        sysOauth=service.saveOrUpdate(sysOauth);
        return super.saveObject(modelMap,sysOauth,"保存成功！","roleUser","保存岗位成员");
    }

    @Open
    public String openEntity(ModelMap modelMap,@PathVariable("id") Long id) {
        TSysRoleUser application=service.getEntity(id);
        return super.openEntity(modelMap,application,"roleUser","查看详情");
    }
    @Edit
    public String edit(ModelMap modelMap,@PathVariable("id") Long s) {
        TSysRoleUser application=service.getEntity(s);
        return super.newIndex(modelMap,application,"roleUser","修改岗位成员");
    }

    @Delete
    public String delete( String ids) {
        return service.delete(ids);
    }

}
